//
//  WXPAuthCameraView.m
//  MexicoLoan
//
//  Created by wxp on 2025/2/28.
//

#import "WXPAuthCameraView.h"

@implementation WXPAuthCameraView

#pragma mark - lazy
- (UIView *)viewBack{
    if (_viewBack == nil) {
        _viewBack = [UIView new];
        _viewBack.backgroundColor = COLOR_WHITE;
        [GlobalMethod setRoundView:_viewBack color:[UIColor clearColor] numRound:16 width:0];
    }
    return _viewBack;
}
- (UILabel *)labelTitle{
    if (_labelTitle == nil) {
        _labelTitle = [UILabel new];
        _labelTitle.textColor = COLOR_BLACK;
        _labelTitle.fontNum = 12;
    }
    return _labelTitle;
}
- (UIImageView *)imageBg{
    if (_imageBg == nil) {
        _imageBg = [UIImageView new];
        _imageBg.image = [UIImage imageNamed:@"icon-KTP-idcard-bg"];
        _imageBg.widthHeight = XY(Ratio(218), Ratio(120));
    }
    return _imageBg;
}
- (UIButton *)btnCamera{
    if (_btnCamera == nil) {
        _btnCamera = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnCamera setImage:[UIImage imageNamed:@"icon-KTP-camera"] forState:UIControlStateNormal];
        _btnCamera.widthHeight = XY(Ratio(60), Ratio(60));
        WEAKSELF
        [_btnCamera setNoRepeatActionWithInterval:1 action:^(UIButton * _Nonnull button) {
            [NSObject clickFeedback];
            [weakSelf.btnCamera addClickAnimate];
            // 打点 - 不管授权与否，点击就打点
            [GlobalMethod hitPointWithPageName:KEY_VerifyKtp action:KEY_ID_Front_Start extInfo:nil];
            [GlobalMethod buryPointWithPageName:KEY_VerifyKtp featureName:KEY_ID_Front_Take action:KEY_BUTTON_CLICK type:KEY_button];
            // 验证相机权限
            [GlobalMethod isOpenCamearPermissionsWithCompletion:^(BOOL granted) {
                if (granted) {
                    if(weakSelf.blockTakePhoto){
                        weakSelf.blockTakePhoto();
                    }
                }
            }];
        }];
    }
    return _btnCamera;
}
- (UILabel *)labelContent{
    if (_labelContent == nil) {
        _labelContent = [UILabel new];
        _labelContent.textColor = COLOR_BLACK;
        _labelContent.fontNum = 12;
    }
    return _labelContent;
}
- (UIImageView *)viewPreview{
    if (_viewPreview == nil) {
        _viewPreview = [UIImageView new];
        _viewPreview.userInteractionEnabled = true;
        _viewPreview.hidden = true;
        _viewPreview.contentMode = UIViewContentModeScaleAspectFill;
        _viewPreview.clipsToBounds = YES;
        _viewPreview.backgroundColor = self.btnSuccess.backgroundColor;
        [GlobalMethod setRoundView:_viewPreview color:[UIColor clearColor] numRound:16 width:0];
        UITapGestureRecognizer * tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
        [_viewPreview addGestureRecognizer:tapGestureRecognizer];
    }
    return _viewPreview;
}
- (UIButton *)btnSuccess{
    if (_btnSuccess == nil) {
        _btnSuccess = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnSuccess.userInteractionEnabled = false;
        _btnSuccess.hidden = true;
        _btnSuccess.backgroundColor = [UIColor clearColor];
        [GlobalMethod setRoundView:_btnSuccess color:[UIColor clearColor] numRound:16 width:0];
        [_btnSuccess setImage:[UIImage imageNamed:@"icon-KTP-cuccess"] forState: UIControlStateNormal];
    }
    return _btnSuccess;
}
- (UIImageView *)viewFail{
    if (_viewFail == nil) {
        _viewFail = [UIImageView new];
        _viewFail.userInteractionEnabled = true;
        _viewFail.hidden = true;
        _viewFail.contentMode = UIViewContentModeScaleAspectFill;
        _viewFail.clipsToBounds = YES;
        _viewFail.backgroundColor = self.btnSuccess.backgroundColor;
        [GlobalMethod setRoundView:_viewFail color:[UIColor clearColor] numRound:16 width:0];
        UITapGestureRecognizer * tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
        [_viewFail addGestureRecognizer:tapGestureRecognizer];
    }
    return _viewFail;
}
- (UIImageView *)imageFail{
    if (_imageFail == nil) {
        _imageFail = [UIImageView new];
        _imageFail.image = [UIImage imageNamed:@"icon-KTP-fail"];
        _imageFail.widthHeight = XY(Ratio(44), Ratio(44));
    }
    return _imageFail;
}
- (UIButton *)btnFail{
    if (_btnFail == nil) {
        _btnFail = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnFail.widthHeight = XY(self.imageBg.width, BUTTON_HEIGHT);
        _btnFail.backgroundColor = COLOR_PRIMARY;
        [GlobalMethod setRoundView:_btnFail color:[UIColor clearColor] numRound:_btnFail.height/2 width:0];
        _btnFail.titleLabel.font = [UIFont systemFontOfSize: 14];
        [_btnFail setTitle:@"Tomar de nuevo" forState:(UIControlStateNormal)];
        [_btnFail setTitleColor:COLOR_BLACK forState:UIControlStateNormal];
        _btnFail.userInteractionEnabled = false;
    }
    return _btnFail;
}
- (WXPAuthExamplePopupView *)examplePopupView{
    if (!_examplePopupView) {
        _examplePopupView = [WXPAuthExamplePopupView new];
    }
    return _examplePopupView;
}

#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.widthHeight = XY(SCREEN_WIDTH, 0);
        self.backgroundColor = [UIColor clearColor];
        [self addSubView];
    }
    return self;
}

- (void)addSubView{
    [self addSubview:self.viewBack];
    [self.viewBack addSubview:self.labelTitle];
    [self.viewBack addSubview:self.imageBg];
    [self.viewBack addSubview:self.btnCamera];
    [self.viewBack addSubview:self.labelContent];
    
    [self addSubview:self.viewPreview];
    [self addSubview:self.btnSuccess];
    
    [self addSubview:self.viewFail];
    [self.viewFail addSubview:self.imageFail];
    [self.viewFail addSubview:self.btnFail];

    [self resetView];
}

#pragma mark -  显示指引
- (void)showExamplePopupView{
    [GlobalMethod mainQueueBlock:^{
        UIWindow * window = [[[UIApplication sharedApplication] delegate] window];
        [window addSubview:self.examplePopupView];
    }];
}

#pragma mark - 手势点击
- (void)tapClick:(UITapGestureRecognizer * )tap{
    [NSObject clickFeedback];
    if(self.blockTakePhoto){
        self.blockTakePhoto();
    }
}

#pragma mark - 刷新View
- (void)resetView{
    self.viewBack.width = SCREEN_WIDTH - 2*LEFT_MARDIN;
    [self.labelTitle fitTitle:@"Haga un escaneo de su identificación, por favor" variable:0];
    self.labelTitle.leftTop = XY(LEFT_MARDIN, Ratio(20));
    
    self.imageBg.centerXTop = XY(self.viewBack.width/2, self.labelTitle.bottom+Ratio(18));
    
    [self.labelContent fitTitle:@"Foto Frontal del INE" variable:0];
    self.labelContent.centerXTop = XY(self.viewBack.width/2, self.imageBg.bottom+Ratio(10));
    self.viewBack.height = self.labelContent.bottom+Ratio(19);
    self.viewBack.centerXTop = XY(self.width/2, 0);
    self.btnCamera.centerXCenterY = XY(self.viewBack.width/2, self.viewBack.height/2);
    // 预览
    self.viewPreview.widthHeight = XY(self.viewBack.width, self.viewBack.height);
    self.viewPreview.centerXCenterY = self.viewBack.centerXCenterY;
    
    // success
    self.btnSuccess.widthHeight = XY(self.viewBack.width, self.viewBack.height);
    self.btnSuccess.centerXCenterY = self.viewBack.centerXCenterY;
    
    // fail
    self.viewFail.widthHeight = XY(self.viewBack.width, self.viewBack.height);
    self.viewFail.centerXCenterY = self.viewBack.centerXCenterY;
    self.imageFail.centerXTop = XY(self.viewFail.width/2, Ratio(65));
    self.btnFail.centerXBottom = XY(self.viewFail.width/2, self.viewFail.height-Ratio(20));
    
    self.height = self.viewBack.bottom+LEFT_MARDIN;
}


- (void)showPreviewImage:(UIImage *)image{
    [self.viewPreview removeAllSubViews];
    UIImageView * imageView = [UIImageView new];
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    imageView.clipsToBounds = YES;
    imageView.widthHeight = XY(self.viewPreview.height, self.viewPreview.width);
    imageView.image = image;
    // 创建一个旋转 90 度的变换
    CGAffineTransform transform = CGAffineTransformMakeRotation(-M_PI_2);
    imageView.transform = transform;
    imageView.leftTop = XY(0, 0);
    [self.viewPreview addSubview:imageView];
}

- (void)resetViewWithShowSuccess:(UIImage *)image{
    [self showPreviewImage:image];
    self.viewPreview.hidden = false;
    self.btnSuccess.hidden = false;
    self.viewFail.hidden = true;
}

- (void)resetViewWithShowFail:(UIImage *)image{
    [self showPreviewImage:image];
    self.viewPreview.hidden = false;
    self.btnSuccess.hidden = true;
    self.viewFail.hidden = false;
}

@end
